﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using WindowsFormsApplication1;
using System.Threading;

namespace Payroll
{
    public partial class Mainform : System.Windows.Forms.Form
    {
        public Mainform()
        {
            StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        public void showTime()//多线程显示系统时间
        {
            while (true)
            {
                DateTime dateTime = DateTime.Now;
                timelabel.Text = string.Format("{0:G}",dateTime);
                Thread.Sleep(1000);
            }
        }

        private void Mainform_Load(object sender, EventArgs e) //打开主界面时加载数据库数据
        {
            Thread P_thread = new Thread(new ThreadStart(showTime));
            P_thread.IsBackground = true;
            P_thread.Start();

            label1.Text = "欢迎用户：" + myUser.Name;

            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();

            string sqlStr = "select * from CommissionedEmployee order by id";
            DataSet dataSet = new DataSet();
            SqlCeDataAdapter sqldata = new SqlCeDataAdapter(sqlStr, connString);
            sqldata.Fill(dataSet, "CommissionedEmployee");
            DataView dataView = new DataView(dataSet.Tables["CommissionedEmployee"]);
            dataGridView1.DataSource = dataView;

            string sqlStr2 = "select * from Hourlyemployee order by id";
            DataSet dataSet2 = new DataSet();
            SqlCeDataAdapter sqldata2 = new SqlCeDataAdapter(sqlStr2, connString);
            sqldata2.Fill(dataSet2, "Hourlyemployee");
            DataView dataView2 = new DataView(dataSet2.Tables["Hourlyemployee"]);
            dataGridView2.DataSource = dataView2;

            string sqlStr3 = "select * from SalariedEmployee order by id";
            DataSet dataSet3 = new DataSet();
            SqlCeDataAdapter sqldata3 = new SqlCeDataAdapter(sqlStr3, connString);
            sqldata3.Fill(dataSet3, "SalariedEmployee");
            DataView dataView3 = new DataView(dataSet3.Tables["SalariedEmployee"]);
            dataGridView3.DataSource = dataView3;

            string sqlStr4 = "select * from TimeCardtable order by date";
            DataSet dataSet4 = new DataSet();
            SqlCeDataAdapter sqldata4 = new SqlCeDataAdapter(sqlStr4, connString);
            sqldata4.Fill(dataSet4, "TimeCardtable");
            DataView dataView4 = new DataView(dataSet4.Tables["TimeCardtable"]);
            dataGridView4.DataSource = dataView4;

            string sqlStr5 = "select * from SalesReceipttable order by date";
            DataSet dataSet5 = new DataSet();
            SqlCeDataAdapter sqldata5 = new SqlCeDataAdapter(sqlStr5, connString);
            sqldata5.Fill(dataSet5, "SalesReceipttable");
            DataView dataView5 = new DataView(dataSet5.Tables["SalesReceipttable"]);
            dataGridView5.DataSource = dataView5;

            string sqlStr6 = "select * from payemployee order by payDate";
            DataSet dataSet6 = new DataSet();
            SqlCeDataAdapter sqldata6 = new SqlCeDataAdapter(sqlStr6, connString);
            sqldata6.Fill(dataSet6, "payemployee");
            DataView dataView6 = new DataView(dataSet6.Tables["payemployee"]);
            dataGridView6.DataSource = dataView6;

            connection.Close();
        }
               

        private void button1_Click(object sender, EventArgs e) //更新员工信息
        {
            string connString = @"Data Source=..\..\Database1.sdf";
            SqlCeConnection connection = new SqlCeConnection(connString);
            connection.Open();

            string sqlStr = "select * from CommissionedEmployee order by id";
            DataSet dataSet = new DataSet();
            SqlCeDataAdapter sqldata = new SqlCeDataAdapter(sqlStr, connString);
            sqldata.Fill(dataSet, "CommissionedEmployee");
            DataView dataView = new DataView(dataSet.Tables["CommissionedEmployee"]);
            dataGridView1.DataSource = dataView;

            string sqlStr2 = "select * from Hourlyemployee order by id";
            DataSet dataSet2 = new DataSet();
            SqlCeDataAdapter sqldata2 = new SqlCeDataAdapter(sqlStr2, connString);
            sqldata2.Fill(dataSet2, "Hourlyemployee");
            DataView dataView2 = new DataView(dataSet2.Tables["Hourlyemployee"]);
            dataGridView2.DataSource = dataView2;

            string sqlStr3 = "select * from SalariedEmployee order by id";
            DataSet dataSet3 = new DataSet();
            SqlCeDataAdapter sqldata3 = new SqlCeDataAdapter(sqlStr3, connString);
            sqldata3.Fill(dataSet3, "SalariedEmployee");
            DataView dataView3 = new DataView(dataSet3.Tables["SalariedEmployee"]);
            dataGridView3.DataSource = dataView3;

            string sqlStr4 = "select * from TimeCardtable order by date";
            DataSet dataSet4 = new DataSet();
            SqlCeDataAdapter sqldata4 = new SqlCeDataAdapter(sqlStr4, connString);
            sqldata4.Fill(dataSet4, "TimeCardtable");
            DataView dataView4 = new DataView(dataSet4.Tables["TimeCardtable"]);
            dataGridView4.DataSource = dataView4;

            string sqlStr5 = "select * from SalesReceipttable order by date";
            DataSet dataSet5 = new DataSet();
            SqlCeDataAdapter sqldata5 = new SqlCeDataAdapter(sqlStr5, connString);
            sqldata5.Fill(dataSet5, "SalesReceipttable");
            DataView dataView5 = new DataView(dataSet5.Tables["SalesReceipttable"]);
            dataGridView5.DataSource = dataView5;

            string sqlStr6 = "select * from payemployee order by payDate";
            DataSet dataSet6 = new DataSet();
            SqlCeDataAdapter sqldata6 = new SqlCeDataAdapter(sqlStr6, connString);
            sqldata6.Fill(dataSet6, "payemployee");
            DataView dataView6 = new DataView(dataSet6.Tables["payemployee"]);
            dataGridView6.DataSource = dataView6;

            connection.Close();
        }

        private void 人员查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Timecard f = new Timecard();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 销售记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Salesreceipt f = new Salesreceipt();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        

        private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            Payemployee f = new Payemployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }



        private void 钟点工ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Hourlyemployee f = new Hourlyemployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 普通员工ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SalariedEmployee f = new SalariedEmployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 销售员工ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CommissionedEmployee f = new CommissionedEmployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 删除员工ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteEmployee f = new DeleteEmployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 更改姓名ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ChangeName f = new ChangeName();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 钟点工ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            CHourlyemployee f = new CHourlyemployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 普通员工ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CSalariedemployee f = new CSalariedemployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 销售员工ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CCommissionedEmployee f = new CCommissionedEmployee();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 工资记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PaiedForm f = new PaiedForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void 员工列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EmployeeListForm f = new EmployeeListForm();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.Show();
        }

        private void Mainform_FormClosing(object sender, FormClosingEventArgs e)
        {
            myFile.myWrite_operate(myUser.Name, "退出");
        }
    }
}
